#include "firstscreen.h"

FirstScreen::FirstScreen(QWidget *parent) :
    QWidget(parent)
{
    startButton = new QPushButton("START",this);
    startButton->resize(200,300);

    username = new QLineEdit(this);
    description = new QLineEdit(this);

    usernameLabel = new QLabel("name",this);
    descLabel = new QLabel("description",this);
    headerLabel = new QLabel("",this);


    lastError = -1;
    lastDifficultyLevel = -1;
    currentDifficultyLevel = 1;

    difficultyComboBox = new QComboBox(this);
    QObject::connect(difficultyComboBox,SIGNAL(currentIndexChanged(QString)),this,SLOT(comboTextChanged(QString)));


    for (int i = 1; i<22; ++i)
    {
	difficultyComboBox->addItem(QString::number(i));
    }
    QLabel* dLabel = new QLabel("difficulty");

    QGridLayout *layout = new QGridLayout(this);
    layout->addWidget(headerLabel,0,0,1,2);

    //TODO: debug only
    layout->addWidget(difficultyComboBox,4,1);
    layout->addWidget(dLabel,4,0);

    layout->addWidget(startButton,1,0,2,2);
    layout->addWidget(usernameLabel,2,0);
    layout->addWidget(username,2,1);
    layout->addWidget(descLabel,3,0);
    layout->addWidget(description,3,1);

    scalpmap = new ScalpMap(1.5,this);
    scalpmap->move((800-scalpmap->width())/2,0);

    QObject::connect(startButton,SIGNAL(pressed()),this,SLOT(startPressed()));


    readLastSessionFile();
}



void FirstScreen::startPressed()
{
    emit startGame();
}

void FirstScreen::cqValue(QString channel, double value)
{

    scalpmap->setCq(channel, value);
}

QString FirstScreen::getUser()
{
    return username->text().trimmed();
}

QString FirstScreen::getDesc()
{
    return description->text().trimmed();
}

void FirstScreen::readLastSessionFile()
{
    QFile* lastF = new QFile(Sbs2Common::getCatalogPath()+"lastSession");
    if (!lastF->open(QIODevice::ReadOnly | QIODevice::Text))
    {
	qDebug() << "problem opening last session information";
	delete lastF;
	return;
    }


    while (!lastF->atEnd())
    {
	QByteArray line = lastF->readLine();
	if (line.split(':').at(0) == "username")
	{
	    lastUser = line.split(':').at(1).trimmed();
	}
	if (line.split(':').at(0) == "lastFile")
	{
	    lastFile = line.split(':').at(1).trimmed();
	}
    }

    lastF->close();
    delete lastF;



    QFile* lastSessionFile = new QFile(lastFile);
    if (!lastSessionFile->open(QIODevice::ReadOnly | QIODevice::Text))
    {
	qDebug() << "problem opening last session file";
	delete lastSessionFile;
	return;
    }

    int usernameSet = 0;
    int difficultySet = 0;
    int errorSet = 0;
    while(!lastSessionFile->atEnd())
    {
        QByteArray line = lastSessionFile->readLine();
	if (line.split(':').at(0) == "username")
	{
	    username->setText(line.split(':').at(1).trimmed());
	    usernameSet = 1;
	}
	if (line.split(':').at(0) == "difficulty")
	{
	   lastDifficultyLevel =  line.split(':').at(1).trimmed().toInt();
	   difficultySet = 1;
	}
	if (line.split(':').at(0) == "error")
	{
	   lastError =  line.split(':').at(1).trimmed().toDouble();
	   errorSet = 1;
	}

	if (usernameSet && difficultySet && errorSet)
	    break;

    }

    lastSessionFile->close();
    delete lastSessionFile;




    calculateCurrentDifficultyLevel();

}

void FirstScreen::calculateCurrentDifficultyLevel()
{

    if (lastError == -1)
	currentDifficultyLevel = 1;
    else
    {
        //TODO:
	//Logic for calculating current difficulty level goes in here
	currentDifficultyLevel = lastDifficultyLevel + 1;

    }

    if (currentDifficultyLevel > 21) currentDifficultyLevel = 21;
    if (currentDifficultyLevel < 1) currentDifficultyLevel = 1;

    for (int i = 0; i < difficultyComboBox->count(); ++i)
    {
	if (difficultyComboBox->itemText(i).toInt() == currentDifficultyLevel)
	    difficultyComboBox->setCurrentIndex(i);

    }

}

void FirstScreen::comboTextChanged(QString text)
{
    currentDifficultyLevel = text.toInt();
}

int FirstScreen::getCurrentDifficultyLevel()
{
    return currentDifficultyLevel;
}
